﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Web.UI.WebControls;
using SmartParking.Data;
using SmartParking.Data.Entities;
using System.Collections.Specialized;

namespace SmartParking.Bussiness
{
    public class PaymentManager
    {
        public static void ProcessPayPalPayment(HttpApplicationState application, HttpRequestBase request, int managerID, bool useSandbox = true)
        {
            var ipAddress = String.Empty;
            try
            {
                PayPalManager.PayPalResponse response = PayPalManager.GetPayPalIPNResponse(request, useSandbox);
                ipAddress = response.Info["custom"];
                switch (response.Status)
                {
                    case PayPalManager.PayPalResponseStatus.Verified:
                        {
                            if (response.Info["payment_status"] == "Completed")
                            {
                                ReservationsManager.SavePayPalWebOrder(application, response, managerID);
                            }
                            else
                            {
                                LogManager.Warning(managerID, ipAddress, String.Format("[PayPal Payment] Status: {0} | TXN_TYPE {1} | TXN_ID: {2}", response.Info["payment_status"], response.Info["txn_type"], response.Info["txn_id"]));
                            }
                            break;
                        }
                    default:
                        {
                            LogManager.Error(managerID, ipAddress, String.Format("[PayPal IPN Error] Status: {0} | TXN_TYPE {1} | TXN_ID: {2}", response.Status, response.Info["txn_type"], response.Info["txn_id"]));
                        }
                        break;
                }
                LogManager.Debug(managerID, ipAddress, String.Format("[PayPal Payment] Status: {0} | TXN_TYPE {1} | TXN_ID: {2} | Session ID: {3}", response.Info["payment_status"], response.Info["txn_type"], response.Info["txn_id"], response.Info["invoice"]));
            }
            catch (Exception ex)
            {
                LogManager.Error(managerID, ipAddress, String.Format("[ProcessPayPalPayment] {0}", ex.Message));
            }            
        }        
    }
}